/**
 * 
 */
package denaris.midlet;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

import denaris.core.Statusbar;
import denaris.factory.ImageFactory;
import denaris.midlet.DenarisGameCanvas.DisplayProperties;

/**
 * @author stkl23
 *
 */
public class GameOverScreen extends AbstractScreen {
	private static GameOverScreen instance;

	private Sprite gameOver = new Sprite(ImageFactory.getImage("/denaris/resource/sprite/game_over.png"), 138, 14);

	private int stillDelay = 10;
	
	public static GameOverScreen getInstance() {
		if (instance == null) {
			instance = new GameOverScreen();
		}

		return instance;
	}
	
	protected GameOverScreen() {
	}

	public void doFullRepaintToBuffer(Graphics g) {
		paintScreenToBuffer(g);
	}

	protected void initialize() {
		// center the sprite just above the top of the screen
		gameOver.setPosition((DisplayProperties.DISPLAY_WIDTH >> 1) - (gameOver.getWidth() >> 1), -gameOver.getHeight());
		stillDelay = 20;
	}

	public void paintScreenToBuffer(Graphics g) {
		g.setColor(0x000000);
		g.fillRect(0, 0, DisplayProperties.DISPLAY_WIDTH, DisplayProperties.DISPLAY_HEIGHT);
		
		gameOver.paint(g);
	}

	public void update() {
		int middle = DisplayProperties.GAME_TOP + (DisplayProperties.GAME_HEIGHT >> 1);
		int deltaY = (middle - gameOver.getY()) >> 2;
		if (deltaY == 0) {
			if (stillDelay == 0) {
				deltaY++;
			}else {
				stillDelay--;
			}
		}
		
		gameOver.move(0, Math.abs(deltaY));
		
		if (gameOver.getY() > DisplayProperties.GAME_TOP + DisplayProperties.GAME_HEIGHT + Statusbar.getInstance().getHeight()) {
			// it's all over now
			Denaris.getInstance().notifyGameOver();
		}
	}

}
